﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using RestSharp;

namespace heyirunde.wx
{
    /// <summary>
    /// setTimeclock 的摘要说明
    /// </summary>
    public class setTimeclock : IHttpHandler
    {

        public  string base_openId;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            JsonHelper jsonHeler = new JsonHelper();
            string state = context.Request.Form["state"];


            /*
            string openId = context.Request.Form["openId"];
            base_openId = openId;

            wxconfig config = new wxconfig();
            config.sendmessage_qiandaotixing(base_openId);
            setTaskAtFixedTime();
            */

            //获取个人推广的二维码大图
          //  string imageurl = wxconfig.getImgUrl("N3YIA2");

            //客服给用户发海报

            /*
            RestClient client7 = new RestClient("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + wxconfig.getToken());
            RestRequest request7 = new RestRequest(Method.POST);

            request7.AddJsonBody(new
            {
                touser = "oEhd9jgzaYI08aSuudnWSIX_hIN4",
                msgtype = "image",
                picurl = "http://114.67.94.169/image/1.jpg",
                media_id ="123"
             
        });

         IRestResponse res =   client7.Execute(request7);

            context.Response.Write(jsonHeler.ObjToJson(new { status = 200, items = res.Content }));
            return;
            */
            

            RestClient client = new RestClient("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+wxconfig.getToken());

            RestRequest request = new RestRequest();
            request.Method = Method.POST;
            request.AddJsonBody(new
            {
                action_name = "QR_LIMIT_STR_SCENE",
                action_info = new
                {
                    scene = new { scene_str = state }
                }
            }
        );
            IRestResponse res = client.Execute(request);

            

            context.Response.Write(jsonHeler.ObjToJson(new { status = 200, items = res.Content }));
            return;
            



        }

        private void setTaskAtFixedTime()
        {
            DateTime now = DateTime.Now;
            DateTime oneOClock = DateTime.Today.AddHours(9.0); //凌晨9：00
            if (now > oneOClock)
            {
                oneOClock = oneOClock.AddDays(1.0);
            }
            int msUntilFour = (int)((oneOClock - now).TotalMilliseconds);

            var t = new System.Threading.Timer(doAt1AM);
            t.Change(msUntilFour, Timeout.Infinite);
        }

        //要执行的任务
        private void doAt1AM(object state)
        {
            //执行功能...
            wxconfig config = new wxconfig();
            config.sendmessage_qiandaotixing(base_openId);

            //再次设定
            setTaskAtFixedTime();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}